package com.qf.fc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.fc.Param.CommentAdd;
import com.qf.fc.Param.CommentWithLike;
import com.qf.fc.Param.ReplayAdd;
import com.qf.fc.dao.CommentDao;
import com.qf.fc.dao.CommentLikeDao;
import com.qf.fc.dto.CommentDto;
import com.qf.fc.entity.Comment;
import com.qf.fc.entity.CommentLike;
import com.qf.fc.service.CommentService;
import com.qf.fc.vo.R;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 评论表(Comment)表服务实现类
 *
 * @author makejava
 * @since 2024-09-23 23:34:47
 */
@Service("commentService")
@RequiredArgsConstructor
public class CommentServiceImpl implements CommentService {
    private final CommentDao dao;

    private final CommentLikeDao commentLikeDao;
    @Override
    public R add(CommentAdd add) {
        //1.验证
        if (add==null && StringUtils.hasLength(add.getContent())){
            return R.fail("亲，请检查参数信息");
        }
        //组装对象，实现数据库操作
        Comment comment = new Comment();
        BeanUtils.copyProperties(add,comment);
        comment.setCreateTime(new Date());
        comment.setParentId(0);

        if (dao.insert(comment)>0){
            return R.ok("");
        }
        return R.fail("亲，网络故障，请检查。");
    }

    @Override
    public R addReplay(ReplayAdd add) {
        //1.验证
        if (add==null && StringUtils.hasLength(add.getContent())){
            return R.fail("亲，请检查参数信息");
        }
        //组装对象，实现数据库操作
        Comment comment = new Comment();
        BeanUtils.copyProperties(add,comment);
        comment.setCreateTime(new Date());

        if (dao.insert(comment)>0){
            return R.ok("");
        }
        return R.fail("亲，网络故障，请检查。");
    }

    @Override
    public R queryComments(Integer mid, Integer page, Integer size, Integer uid) {
        Map<String, Object> result = new HashMap<>();
        Long total = dao.selectCount(mid);
        List<CommentDto> commentList = dao.selectLimit(mid, (page - 1) * size, size);

        // 提取评论的 id 列表
        List<Integer> commentIds = commentList.stream()
                .map(CommentDto::getId)
                .collect(Collectors.toList());

        // 根据评论 id 列表和 uid 查询点赞信息
        LambdaQueryWrapper<CommentLike> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(CommentLike::getCid, commentIds)
                .eq(CommentLike::getUid, uid);
        List<CommentLike> commentLikes = commentLikeDao.selectList(queryWrapper);

        // 将点赞信息转换为 Map，方便查找
        Set<Integer> likedCommentIds = commentLikes.stream()
                .map(CommentLike::getCid)
                .collect(Collectors.toSet());

        // 创建带有点赞标记的评论列表
        List<CommentWithLike> commentWithLikeList = new ArrayList<>();
        for (CommentDto comment : commentList) {
            boolean liked = likedCommentIds.contains(comment.getId());
            commentWithLikeList.add(new CommentWithLike(comment, liked));
        }

        result.put("total", total);
        result.put("data", commentWithLikeList);
        result.put("pages", total % size == 0 ? total / size : total / size + 1);
        return R.ok(result);
    }

    @Override
    public R queryMy(Integer uid) {
        return R.ok(dao.selectList(new LambdaQueryWrapper<Comment>().eq(Comment::getUid,uid)));
    }
}

